
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>类型兼容性 · TypeScript Handbook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Patrick Zhong">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Advanced Types.html" />
    
    
    <link rel="prev" href="Type Inference.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="tutorials/">
            
                <a href="tutorials/">
            
                    
                    快速上手
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="tutorials/TypeScript in 5 minutes.html">
            
                <a href="tutorials/TypeScript in 5 minutes.html">
            
                    
                    5分钟了解TypeScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="tutorials/ASP.NET Core.html">
            
                <a href="tutorials/ASP.NET Core.html">
            
                    
                    ASP.NET Core
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="tutorials/ASP.NET 4.html">
            
                <a href="tutorials/ASP.NET 4.html">
            
                    
                    ASP.NET 4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="tutorials/Gulp.html">
            
                <a href="tutorials/Gulp.html">
            
                    
                    Gulp
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="tutorials/Knockout.html">
            
                <a href="tutorials/Knockout.html">
            
                    
                    Knockout.js
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="tutorials/React & Webpack.html">
            
                <a href="tutorials/React & Webpack.html">
            
                    
                    React与webpack
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="tutorials/React.html">
            
                <a href="tutorials/React.html">
            
                    
                    React
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="tutorials/Angular 2.html">
            
                <a href="tutorials/Angular 2.html">
            
                    
                    Angular 2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="tutorials/Migrating from JavaScript.html">
            
                <a href="tutorials/Migrating from JavaScript.html">
            
                    
                    从JavaScript迁移到TypeScript
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="./">
            
                <a href="./">
            
                    
                    手册
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="Basic Types.html">
            
                <a href="Basic Types.html">
            
                    
                    基础类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="Variable Declarations.html">
            
                <a href="Variable Declarations.html">
            
                    
                    变量声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="Interfaces.html">
            
                <a href="Interfaces.html">
            
                    
                    接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="Classes.html">
            
                <a href="Classes.html">
            
                    
                    类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="Functions.html">
            
                <a href="Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="Generics.html">
            
                <a href="Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="Enums.html">
            
                <a href="Enums.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="Type Inference.html">
            
                <a href="Type Inference.html">
            
                    
                    类型推论
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.9" data-path="Type Compatibility.html">
            
                <a href="Type Compatibility.html">
            
                    
                    类型兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="Advanced Types.html">
            
                <a href="Advanced Types.html">
            
                    
                    高级类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="Utilit Types.md">
            
                <span>
            
                    
                    实用工具类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="Symbols.html">
            
                <a href="Symbols.html">
            
                    
                    Symbols
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="Iterators and Generators.html">
            
                <a href="Iterators and Generators.html">
            
                    
                    Iterators 和 Generators
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="Modules.html">
            
                <a href="Modules.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="Namespaces.html">
            
                <a href="Namespaces.html">
            
                    
                    命名空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="Namespaces and Modules.html">
            
                <a href="Namespaces and Modules.html">
            
                    
                    命名空间和模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="Module Resolution.html">
            
                <a href="Module Resolution.html">
            
                    
                    模块解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="Declaration Merging.html">
            
                <a href="Declaration Merging.html">
            
                    
                    声明合并
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="Writing Definition Files.html">
            
                <a href="Writing Definition Files.html">
            
                    
                    书写.d.ts文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="JSX.html">
            
                <a href="JSX.html">
            
                    
                    JSX
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="Decorators.html">
            
                <a href="Decorators.html">
            
                    
                    Decorators
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="Mixins.html">
            
                <a href="Mixins.html">
            
                    
                    混入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="Triple-Slash Directives.html">
            
                <a href="Triple-Slash Directives.html">
            
                    
                    三斜线指令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="Type Checking JavaScript Files.html">
            
                <a href="Type Checking JavaScript Files.html">
            
                    
                    JavaScript文件里的类型检查
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="declaration files/Introduction.html">
            
                <a href="declaration files/Introduction.html">
            
                    
                    如何书写声明文件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="declaration files/Library Structures.html">
            
                <a href="declaration files/Library Structures.html">
            
                    
                    结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="declaration files/Do's and Don'ts.html">
            
                <a href="declaration files/Do's and Don'ts.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="declaration files/By Example.html">
            
                <a href="declaration files/By Example.html">
            
                    
                    举例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="declaration files/Deep Dive.html">
            
                <a href="declaration files/Deep Dive.html">
            
                    
                    深入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="declaration files/Publishing.html">
            
                <a href="declaration files/Publishing.html">
            
                    
                    发布
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="declaration files/Consumption.html">
            
                <a href="declaration files/Consumption.html">
            
                    
                    使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="tsconfig.json.html">
            
                <a href="tsconfig.json.html">
            
                    
                    工程配置
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="tsconfig.json.html">
            
                <a href="tsconfig.json.html">
            
                    
                    tsconfig.json
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="Project References.html">
            
                <a href="Project References.html">
            
                    
                    工程引用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="Typings for NPM Packages.html">
            
                <a href="Typings for NPM Packages.html">
            
                    
                    NPM包的类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="Compiler Options.html">
            
                <a href="Compiler Options.html">
            
                    
                    编译选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="Configuring Watch.html">
            
                <a href="Configuring Watch.html">
            
                    
                    配置 Watch
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="Compiler Options in MSBuild.html">
            
                <a href="Compiler Options in MSBuild.html">
            
                    
                    在MSBuild里使用编译选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="Integrating with Build Tools.html">
            
                <a href="Integrating with Build Tools.html">
            
                    
                    与其它构建工具整合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="Nightly Builds.html">
            
                <a href="Nightly Builds.html">
            
                    
                    使用TypeScript的每日构建版本
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../wiki/">
            
                <a href="../wiki/">
            
                    
                    Wiki
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../wiki/this-in-TypeScript.html">
            
                <a href="../wiki/this-in-TypeScript.html">
            
                    
                    TypeScript里的this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../wiki/coding_guidelines.html">
            
                <a href="../wiki/coding_guidelines.html">
            
                    
                    编码规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../wiki/Common Errors.html">
            
                <a href="../wiki/Common Errors.html">
            
                    
                    常见编译错误
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../wiki/TypeScript-Editor-Support.html">
            
                <a href="../wiki/TypeScript-Editor-Support.html">
            
                    
                    支持TypeScript的编辑器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../wiki/Using-TypeScript-With-ASP.NET-5.html">
            
                <a href="../wiki/Using-TypeScript-With-ASP.NET-5.html">
            
                    
                    结合ASP.NET v5使用TypeScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../wiki/Architectural-Overview.html">
            
                <a href="../wiki/Architectural-Overview.html">
            
                    
                    架构概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../wiki/Roadmap.html">
            
                <a href="../wiki/Roadmap.html">
            
                    
                    发展路线图
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../release-notes/">
            
                <a href="../release-notes/">
            
                    
                    新增功能
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../release-notes/TypeScript 3.1.html">
            
                <a href="../release-notes/TypeScript 3.1.html">
            
                    
                    TypeScript 3.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../release-notes/TypeScript 3.0.html">
            
                <a href="../release-notes/TypeScript 3.0.html">
            
                    
                    TypeScript 3.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../release-notes/TypeScript 2.9.html">
            
                <a href="../release-notes/TypeScript 2.9.html">
            
                    
                    TypeScript 2.9
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../release-notes/TypeScript 2.8.html">
            
                <a href="../release-notes/TypeScript 2.8.html">
            
                    
                    TypeScript 2.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../release-notes/TypeScript 2.7.html">
            
                <a href="../release-notes/TypeScript 2.7.html">
            
                    
                    TypeScript 2.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../release-notes/TypeScript 2.6.html">
            
                <a href="../release-notes/TypeScript 2.6.html">
            
                    
                    TypeScript 2.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="../release-notes/TypeScript 2.5.html">
            
                <a href="../release-notes/TypeScript 2.5.html">
            
                    
                    TypeScript 2.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.8" data-path="../release-notes/TypeScript 2.4.html">
            
                <a href="../release-notes/TypeScript 2.4.html">
            
                    
                    TypeScript 2.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.9" data-path="../release-notes/TypeScript 2.3.html">
            
                <a href="../release-notes/TypeScript 2.3.html">
            
                    
                    TypeScript 2.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.10" data-path="../release-notes/TypeScript 2.2.html">
            
                <a href="../release-notes/TypeScript 2.2.html">
            
                    
                    TypeScript 2.2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.11" data-path="../release-notes/TypeScript 2.1.html">
            
                <a href="../release-notes/TypeScript 2.1.html">
            
                    
                    TypeScript 2.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.12" data-path="../release-notes/TypeScript 2.0.html">
            
                <a href="../release-notes/TypeScript 2.0.html">
            
                    
                    TypeScript 2.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.13" data-path="../release-notes/TypeScript 1.8.html">
            
                <a href="../release-notes/TypeScript 1.8.html">
            
                    
                    TypeScript 1.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.14" data-path="../release-notes/TypeScript 1.7.html">
            
                <a href="../release-notes/TypeScript 1.7.html">
            
                    
                    TypeScript 1.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.15" data-path="../release-notes/TypeScript 1.6.html">
            
                <a href="../release-notes/TypeScript 1.6.html">
            
                    
                    TypeScript 1.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.16" data-path="../release-notes/TypeScript 1.5.html">
            
                <a href="../release-notes/TypeScript 1.5.html">
            
                    
                    TypeScript 1.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.17" data-path="../release-notes/TypeScript 1.4.html">
            
                <a href="../release-notes/TypeScript 1.4.html">
            
                    
                    TypeScript 1.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.18" data-path="../release-notes/TypeScript 1.3.html">
            
                <a href="../release-notes/TypeScript 1.3.html">
            
                    
                    TypeScript 1.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.19" data-path="../release-notes/TypeScript 1.1.html">
            
                <a href="../release-notes/TypeScript 1.1.html">
            
                    
                    TypeScript 1.1
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../breaking-changes/breaking-changes.html">
            
                <a href="../breaking-changes/breaking-changes.html">
            
                    
                    Breaking Changes
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../breaking-changes/TypeScript 2.4.html">
            
                <a href="../breaking-changes/TypeScript 2.4.html">
            
                    
                    TypeScript 2.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../breaking-changes/TypeScript 2.3.html">
            
                <a href="../breaking-changes/TypeScript 2.3.html">
            
                    
                    TypeScript 2.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../breaking-changes/TypeScript 2.2.html">
            
                <a href="../breaking-changes/TypeScript 2.2.html">
            
                    
                    TypeScript 2.2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../breaking-changes/TypeScript 2.1.html">
            
                <a href="../breaking-changes/TypeScript 2.1.html">
            
                    
                    TypeScript 2.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../breaking-changes/TypeScript 2.0.html">
            
                <a href="../breaking-changes/TypeScript 2.0.html">
            
                    
                    TypeScript 2.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="../breaking-changes/TypeScript 1.8.html">
            
                <a href="../breaking-changes/TypeScript 1.8.html">
            
                    
                    TypeScript 1.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="../breaking-changes/TypeScript 1.7.html">
            
                <a href="../breaking-changes/TypeScript 1.7.html">
            
                    
                    TypeScript 1.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="../breaking-changes/TypeScript 1.6.html">
            
                <a href="../breaking-changes/TypeScript 1.6.html">
            
                    
                    TypeScript 1.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.9" data-path="../breaking-changes/TypeScript 1.5.html">
            
                <a href="../breaking-changes/TypeScript 1.5.html">
            
                    
                    TypeScript 1.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.10" data-path="../breaking-changes/TypeScript 1.4.html">
            
                <a href="../breaking-changes/TypeScript 1.4.html">
            
                    
                    TypeScript 1.4
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >类型兼容性</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x4ECB;&#x7ECD;">&#x4ECB;&#x7ECD;</h1>
<p>TypeScript&#x91CC;&#x7684;&#x7C7B;&#x578B;&#x517C;&#x5BB9;&#x6027;&#x662F;&#x57FA;&#x4E8E;&#x7ED3;&#x6784;&#x5B50;&#x7C7B;&#x578B;&#x7684;&#x3002;
&#x7ED3;&#x6784;&#x7C7B;&#x578B;&#x662F;&#x4E00;&#x79CD;&#x53EA;&#x4F7F;&#x7528;&#x5176;&#x6210;&#x5458;&#x6765;&#x63CF;&#x8FF0;&#x7C7B;&#x578B;&#x7684;&#x65B9;&#x5F0F;&#x3002;
&#x5B83;&#x6B63;&#x597D;&#x4E0E;&#x540D;&#x4E49;&#xFF08;nominal&#xFF09;&#x7C7B;&#x578B;&#x5F62;&#x6210;&#x5BF9;&#x6BD4;&#x3002;&#xFF08;&#x8BD1;&#x8005;&#x6CE8;&#xFF1A;&#x5728;&#x57FA;&#x4E8E;&#x540D;&#x4E49;&#x7C7B;&#x578B;&#x7684;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x4E2D;&#xFF0C;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x7684;&#x517C;&#x5BB9;&#x6027;&#x6216;&#x7B49;&#x4EF7;&#x6027;&#x662F;&#x901A;&#x8FC7;&#x660E;&#x786E;&#x7684;&#x58F0;&#x660E;&#x548C;/&#x6216;&#x7C7B;&#x578B;&#x7684;&#x540D;&#x79F0;&#x6765;&#x51B3;&#x5B9A;&#x7684;&#x3002;&#x8FD9;&#x4E0E;&#x7ED3;&#x6784;&#x6027;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x4E0D;&#x540C;&#xFF0C;&#x5B83;&#x662F;&#x57FA;&#x4E8E;&#x7C7B;&#x578B;&#x7684;&#x7EC4;&#x6210;&#x7ED3;&#x6784;&#xFF0C;&#x4E14;&#x4E0D;&#x8981;&#x6C42;&#x660E;&#x786E;&#x5730;&#x58F0;&#x660E;&#x3002;&#xFF09;
&#x770B;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> Named {
    name: <span class="hljs-built_in">string</span>;
}

<span class="hljs-keyword">class</span> Person {
    name: <span class="hljs-built_in">string</span>;
}

<span class="hljs-keyword">let</span> p: Named;
<span class="hljs-comment">// OK, because of structural typing</span>
p = <span class="hljs-keyword">new</span> Person();
</code></pre>
<p>&#x5728;&#x4F7F;&#x7528;&#x57FA;&#x4E8E;&#x540D;&#x4E49;&#x7C7B;&#x578B;&#x7684;&#x8BED;&#x8A00;&#xFF0C;&#x6BD4;&#x5982;C#&#x6216;Java&#x4E2D;&#xFF0C;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x4F1A;&#x62A5;&#x9519;&#xFF0C;&#x56E0;&#x4E3A;Person&#x7C7B;&#x6CA1;&#x6709;&#x660E;&#x786E;&#x8BF4;&#x660E;&#x5176;&#x5B9E;&#x73B0;&#x4E86;Named&#x63A5;&#x53E3;&#x3002;</p>
<p>TypeScript&#x7684;&#x7ED3;&#x6784;&#x6027;&#x5B50;&#x7C7B;&#x578B;&#x662F;&#x6839;&#x636E;JavaScript&#x4EE3;&#x7801;&#x7684;&#x5178;&#x578B;&#x5199;&#x6CD5;&#x6765;&#x8BBE;&#x8BA1;&#x7684;&#x3002;
&#x56E0;&#x4E3A;JavaScript&#x91CC;&#x5E7F;&#x6CDB;&#x5730;&#x4F7F;&#x7528;&#x533F;&#x540D;&#x5BF9;&#x8C61;&#xFF0C;&#x4F8B;&#x5982;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#x548C;&#x5BF9;&#x8C61;&#x5B57;&#x9762;&#x91CF;&#xFF0C;&#x6240;&#x4EE5;&#x4F7F;&#x7528;&#x7ED3;&#x6784;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x6765;&#x63CF;&#x8FF0;&#x8FD9;&#x4E9B;&#x7C7B;&#x578B;&#x6BD4;&#x4F7F;&#x7528;&#x540D;&#x4E49;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x66F4;&#x597D;&#x3002;</p>
<h2 id="&#x5173;&#x4E8E;&#x53EF;&#x9760;&#x6027;&#x7684;&#x6CE8;&#x610F;&#x4E8B;&#x9879;">&#x5173;&#x4E8E;&#x53EF;&#x9760;&#x6027;&#x7684;&#x6CE8;&#x610F;&#x4E8B;&#x9879;</h2>
<p>TypeScript&#x7684;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x5141;&#x8BB8;&#x67D0;&#x4E9B;&#x5728;&#x7F16;&#x8BD1;&#x9636;&#x6BB5;&#x65E0;&#x6CD5;&#x786E;&#x8BA4;&#x5176;&#x5B89;&#x5168;&#x6027;&#x7684;&#x64CD;&#x4F5C;&#x3002;&#x5F53;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x5177;&#x6B64;&#x5C5E;&#x6027;&#x65F6;&#xFF0C;&#x88AB;&#x5F53;&#x505A;&#x662F;&#x201C;&#x4E0D;&#x53EF;&#x9760;&#x201D;&#x7684;&#x3002;TypeScript&#x5141;&#x8BB8;&#x8FD9;&#x79CD;&#x4E0D;&#x53EF;&#x9760;&#x884C;&#x4E3A;&#x7684;&#x53D1;&#x751F;&#x662F;&#x7ECF;&#x8FC7;&#x4ED4;&#x7EC6;&#x8003;&#x8651;&#x7684;&#x3002;&#x901A;&#x8FC7;&#x8FD9;&#x7BC7;&#x6587;&#x7AE0;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x89E3;&#x91CA;&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x4F1A;&#x53D1;&#x751F;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x548C;&#x5176;&#x6709;&#x5229;&#x7684;&#x4E00;&#x9762;&#x3002;</p>
<h1 id="&#x5F00;&#x59CB;">&#x5F00;&#x59CB;</h1>
<p>TypeScript&#x7ED3;&#x6784;&#x5316;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x7684;&#x57FA;&#x672C;&#x89C4;&#x5219;&#x662F;&#xFF0C;&#x5982;&#x679C;<code>x</code>&#x8981;&#x517C;&#x5BB9;<code>y</code>&#xFF0C;&#x90A3;&#x4E48;<code>y</code>&#x81F3;&#x5C11;&#x5177;&#x6709;&#x4E0E;<code>x</code>&#x76F8;&#x540C;&#x7684;&#x5C5E;&#x6027;&#x3002;&#x6BD4;&#x5982;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> Named {
    name: <span class="hljs-built_in">string</span>;
}

<span class="hljs-keyword">let</span> x: Named;
<span class="hljs-comment">// y&apos;s inferred type is { name: string; location: string; }</span>
<span class="hljs-keyword">let</span> y = { name: <span class="hljs-string">&apos;Alice&apos;</span>, location: <span class="hljs-string">&apos;Seattle&apos;</span> };
x = y;
</code></pre>
<p>&#x8FD9;&#x91CC;&#x8981;&#x68C0;&#x67E5;<code>y</code>&#x662F;&#x5426;&#x80FD;&#x8D4B;&#x503C;&#x7ED9;<code>x</code>&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x68C0;&#x67E5;<code>x</code>&#x4E2D;&#x7684;&#x6BCF;&#x4E2A;&#x5C5E;&#x6027;&#xFF0C;&#x770B;&#x662F;&#x5426;&#x80FD;&#x5728;<code>y</code>&#x4E2D;&#x4E5F;&#x627E;&#x5230;&#x5BF9;&#x5E94;&#x5C5E;&#x6027;&#x3002;
&#x5728;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;<code>y</code>&#x5FC5;&#x987B;&#x5305;&#x542B;&#x540D;&#x5B57;&#x662F;<code>name</code>&#x7684;<code>string</code>&#x7C7B;&#x578B;&#x6210;&#x5458;&#x3002;<code>y</code>&#x6EE1;&#x8DB3;&#x6761;&#x4EF6;&#xFF0C;&#x56E0;&#x6B64;&#x8D4B;&#x503C;&#x6B63;&#x786E;&#x3002;</p>
<p>&#x68C0;&#x67E5;&#x51FD;&#x6570;&#x53C2;&#x6570;&#x65F6;&#x4F7F;&#x7528;&#x76F8;&#x540C;&#x7684;&#x89C4;&#x5219;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params">n: Named</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Hello, &apos;</span> + n.name);
}
greet(y); <span class="hljs-comment">// OK</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;<code>y</code>&#x6709;&#x4E2A;&#x989D;&#x5916;&#x7684;<code>location</code>&#x5C5E;&#x6027;&#xFF0C;&#x4F46;&#x8FD9;&#x4E0D;&#x4F1A;&#x5F15;&#x53D1;&#x9519;&#x8BEF;&#x3002;
&#x53EA;&#x6709;&#x76EE;&#x6807;&#x7C7B;&#x578B;&#xFF08;&#x8FD9;&#x91CC;&#x662F;<code>Named</code>&#xFF09;&#x7684;&#x6210;&#x5458;&#x4F1A;&#x88AB;&#x4E00;&#x4E00;&#x68C0;&#x67E5;&#x662F;&#x5426;&#x517C;&#x5BB9;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x6BD4;&#x8F83;&#x8FC7;&#x7A0B;&#x662F;&#x9012;&#x5F52;&#x8FDB;&#x884C;&#x7684;&#xFF0C;&#x68C0;&#x67E5;&#x6BCF;&#x4E2A;&#x6210;&#x5458;&#x53CA;&#x5B50;&#x6210;&#x5458;&#x3002;</p>
<h1 id="&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x51FD;&#x6570;">&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x51FD;&#x6570;</h1>
<p>&#x76F8;&#x5BF9;&#x6765;&#x8BB2;&#xFF0C;&#x5728;&#x6BD4;&#x8F83;&#x539F;&#x59CB;&#x7C7B;&#x578B;&#x548C;&#x5BF9;&#x8C61;&#x7C7B;&#x578B;&#x7684;&#x65F6;&#x5019;&#x662F;&#x6BD4;&#x8F83;&#x5BB9;&#x6613;&#x7406;&#x89E3;&#x7684;&#xFF0C;&#x95EE;&#x9898;&#x662F;&#x5982;&#x4F55;&#x5224;&#x65AD;&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x662F;&#x517C;&#x5BB9;&#x7684;&#x3002;
&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x4ECE;&#x4E24;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x51FD;&#x6570;&#x5165;&#x624B;&#xFF0C;&#x5B83;&#x4EEC;&#x4EC5;&#x662F;&#x53C2;&#x6570;&#x5217;&#x8868;&#x7565;&#x6709;&#x4E0D;&#x540C;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> x = (a: <span class="hljs-built_in">number</span>) =&gt; <span class="hljs-number">0</span>;
<span class="hljs-keyword">let</span> y = (b: <span class="hljs-built_in">number</span>, s: <span class="hljs-built_in">string</span>) =&gt; <span class="hljs-number">0</span>;

y = x; <span class="hljs-comment">// OK</span>
x = y; <span class="hljs-comment">// Error</span>
</code></pre>
<p>&#x8981;&#x67E5;&#x770B;<code>x</code>&#x662F;&#x5426;&#x80FD;&#x8D4B;&#x503C;&#x7ED9;<code>y</code>&#xFF0C;&#x9996;&#x5148;&#x770B;&#x5B83;&#x4EEC;&#x7684;&#x53C2;&#x6570;&#x5217;&#x8868;&#x3002;
<code>x</code>&#x7684;&#x6BCF;&#x4E2A;&#x53C2;&#x6570;&#x5FC5;&#x987B;&#x80FD;&#x5728;<code>y</code>&#x91CC;&#x627E;&#x5230;&#x5BF9;&#x5E94;&#x7C7B;&#x578B;&#x7684;&#x53C2;&#x6570;&#x3002;
&#x6CE8;&#x610F;&#x7684;&#x662F;&#x53C2;&#x6570;&#x7684;&#x540D;&#x5B57;&#x76F8;&#x540C;&#x4E0E;&#x5426;&#x65E0;&#x6240;&#x8C13;&#xFF0C;&#x53EA;&#x770B;&#x5B83;&#x4EEC;&#x7684;&#x7C7B;&#x578B;&#x3002;
&#x8FD9;&#x91CC;&#xFF0C;<code>x</code>&#x7684;&#x6BCF;&#x4E2A;&#x53C2;&#x6570;&#x5728;<code>y</code>&#x4E2D;&#x90FD;&#x80FD;&#x627E;&#x5230;&#x5BF9;&#x5E94;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x6240;&#x4EE5;&#x5141;&#x8BB8;&#x8D4B;&#x503C;&#x3002;</p>
<p>&#x7B2C;&#x4E8C;&#x4E2A;&#x8D4B;&#x503C;&#x9519;&#x8BEF;&#xFF0C;&#x56E0;&#x4E3A;<code>y</code>&#x6709;&#x4E2A;&#x5FC5;&#x9700;&#x7684;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x4F46;&#x662F;<code>x</code>&#x5E76;&#x6CA1;&#x6709;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x5141;&#x8BB8;&#x8D4B;&#x503C;&#x3002;</p>
<p>&#x4F60;&#x53EF;&#x80FD;&#x4F1A;&#x7591;&#x60D1;&#x4E3A;&#x4EC0;&#x4E48;&#x5141;&#x8BB8;<code>&#x5FFD;&#x7565;</code>&#x53C2;&#x6570;&#xFF0C;&#x50CF;&#x4F8B;&#x5B50;<code>y = x</code>&#x4E2D;&#x90A3;&#x6837;&#x3002;
&#x539F;&#x56E0;&#x662F;&#x5FFD;&#x7565;&#x989D;&#x5916;&#x7684;&#x53C2;&#x6570;&#x5728;JavaScript&#x91CC;&#x662F;&#x5F88;&#x5E38;&#x89C1;&#x7684;&#x3002;
&#x4F8B;&#x5982;&#xFF0C;<code>Array#forEach</code>&#x7ED9;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4F20;3&#x4E2A;&#x53C2;&#x6570;&#xFF1A;&#x6570;&#x7EC4;&#x5143;&#x7D20;&#xFF0C;&#x7D22;&#x5F15;&#x548C;&#x6574;&#x4E2A;&#x6570;&#x7EC4;&#x3002;
&#x5C3D;&#x7BA1;&#x5982;&#x6B64;&#xFF0C;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x53EA;&#x4F7F;&#x7528;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4E5F;&#x662F;&#x5F88;&#x6709;&#x7528;&#x7684;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> items = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>];

<span class="hljs-comment">// Don&apos;t force these extra arguments</span>
items.forEach((item, index, array) =&gt; <span class="hljs-built_in">console</span>.log(item));

<span class="hljs-comment">// Should be OK!</span>
items.forEach((item) =&gt; <span class="hljs-built_in">console</span>.log(item));
</code></pre>
<p>&#x4E0B;&#x9762;&#x6765;&#x770B;&#x770B;&#x5982;&#x4F55;&#x5904;&#x7406;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#xFF0C;&#x521B;&#x5EFA;&#x4E24;&#x4E2A;&#x4EC5;&#x662F;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#x4E0D;&#x540C;&#x7684;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> x = () =&gt; ({name: <span class="hljs-string">&apos;Alice&apos;</span>});
<span class="hljs-keyword">let</span> y = () =&gt; ({name: <span class="hljs-string">&apos;Alice&apos;</span>, location: <span class="hljs-string">&apos;Seattle&apos;</span>});

x = y; <span class="hljs-comment">// OK</span>
y = x; <span class="hljs-comment">// Error, because x() lacks a location property</span>
</code></pre>
<p>&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x5F3A;&#x5236;&#x6E90;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#x5FC5;&#x987B;&#x662F;&#x76EE;&#x6807;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#x7684;&#x5B50;&#x7C7B;&#x578B;&#x3002;</p>
<h2 id="&#x51FD;&#x6570;&#x53C2;&#x6570;&#x53CC;&#x5411;&#x534F;&#x53D8;">&#x51FD;&#x6570;&#x53C2;&#x6570;&#x53CC;&#x5411;&#x534F;&#x53D8;</h2>
<p>&#x5F53;&#x6BD4;&#x8F83;&#x51FD;&#x6570;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x53EA;&#x6709;&#x5F53;&#x6E90;&#x51FD;&#x6570;&#x53C2;&#x6570;&#x80FD;&#x591F;&#x8D4B;&#x503C;&#x7ED9;&#x76EE;&#x6807;&#x51FD;&#x6570;&#x6216;&#x8005;&#x53CD;&#x8FC7;&#x6765;&#x65F6;&#x624D;&#x80FD;&#x8D4B;&#x503C;&#x6210;&#x529F;&#x3002;
&#x8FD9;&#x662F;&#x4E0D;&#x7A33;&#x5B9A;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x8C03;&#x7528;&#x8005;&#x53EF;&#x80FD;&#x4F20;&#x5165;&#x4E86;&#x4E00;&#x4E2A;&#x5177;&#x6709;&#x66F4;&#x7CBE;&#x786E;&#x7C7B;&#x578B;&#x4FE1;&#x606F;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x4F46;&#x662F;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x4F20;&#x5165;&#x7684;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#x5374;&#x4F7F;&#x7528;&#x4E86;&#x4E0D;&#x662F;&#x90A3;&#x4E48;&#x7CBE;&#x786E;&#x7684;&#x7C7B;&#x578B;&#x4FE1;&#x606F;&#x3002;
&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;&#x8FD9;&#x6781;&#x5C11;&#x4F1A;&#x53D1;&#x751F;&#x9519;&#x8BEF;&#xFF0C;&#x5E76;&#x4E14;&#x80FD;&#x591F;&#x5B9E;&#x73B0;&#x5F88;&#x591A;JavaScript&#x91CC;&#x7684;&#x5E38;&#x89C1;&#x6A21;&#x5F0F;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-ts">enum EventType { Mouse, Keyboard }

interface Event { timestamp: number; }
interface MouseEvent extends Event { x: number; y: number }
interface KeyEvent extends Event { keyCode: number }

function listenEvent(eventType: EventType, handler: (n: Event) =&gt; void) {
    /* ... */
}

// Unsound, but useful and common
listenEvent(EventType.Mouse, (e: MouseEvent) =&gt; console.log(e.x + &apos;,&apos; + e.y));

// Undesirable alternatives in presence of soundness
listenEvent(EventType.Mouse, (e: Event) =&gt; console.log((&lt;MouseEvent&gt;e).x + &apos;,&apos; + (&lt;MouseEvent&gt;e).y));
listenEvent(EventType.Mouse, &lt;(e: Event) =&gt; void&gt;((e: MouseEvent) =&gt; console.log(e.x + &apos;,&apos; + e.y)));

// Still disallowed (clear error). Type safety enforced for wholly incompatible types
listenEvent(EventType.Mouse, (e: number) =&gt; console.log(e));
</code></pre>
<h2 id="&#x53EF;&#x9009;&#x53C2;&#x6570;&#x53CA;&#x5269;&#x4F59;&#x53C2;&#x6570;">&#x53EF;&#x9009;&#x53C2;&#x6570;&#x53CA;&#x5269;&#x4F59;&#x53C2;&#x6570;</h2>
<p>&#x6BD4;&#x8F83;&#x51FD;&#x6570;&#x517C;&#x5BB9;&#x6027;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EF;&#x9009;&#x53C2;&#x6570;&#x4E0E;&#x5FC5;&#x987B;&#x53C2;&#x6570;&#x662F;&#x53EF;&#x4E92;&#x6362;&#x7684;&#x3002;
&#x6E90;&#x7C7B;&#x578B;&#x4E0A;&#x6709;&#x989D;&#x5916;&#x7684;&#x53EF;&#x9009;&#x53C2;&#x6570;&#x4E0D;&#x662F;&#x9519;&#x8BEF;&#xFF0C;&#x76EE;&#x6807;&#x7C7B;&#x578B;&#x7684;&#x53EF;&#x9009;&#x53C2;&#x6570;&#x5728;&#x6E90;&#x7C7B;&#x578B;&#x91CC;&#x6CA1;&#x6709;&#x5BF9;&#x5E94;&#x7684;&#x53C2;&#x6570;&#x4E5F;&#x4E0D;&#x662F;&#x9519;&#x8BEF;&#x3002;</p>
<p>&#x5F53;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x6709;&#x5269;&#x4F59;&#x53C2;&#x6570;&#x65F6;&#xFF0C;&#x5B83;&#x88AB;&#x5F53;&#x505A;&#x65E0;&#x9650;&#x4E2A;&#x53EF;&#x9009;&#x53C2;&#x6570;&#x3002;</p>
<p>&#x8FD9;&#x5BF9;&#x4E8E;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x6765;&#x8BF4;&#x662F;&#x4E0D;&#x7A33;&#x5B9A;&#x7684;&#xFF0C;&#x4F46;&#x4ECE;&#x8FD0;&#x884C;&#x65F6;&#x7684;&#x89D2;&#x5EA6;&#x6765;&#x770B;&#xFF0C;&#x53EF;&#x9009;&#x53C2;&#x6570;&#x4E00;&#x822C;&#x6765;&#x8BF4;&#x662F;&#x4E0D;&#x5F3A;&#x5236;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x5BF9;&#x4E8E;&#x5927;&#x591A;&#x6570;&#x51FD;&#x6570;&#x6765;&#x8BF4;&#x76F8;&#x5F53;&#x4E8E;&#x4F20;&#x9012;&#x4E86;&#x4E00;&#x4E9B;<code>undefinded</code>&#x3002;</p>
<p>&#x6709;&#x4E00;&#x4E2A;&#x597D;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x5E38;&#x89C1;&#x7684;&#x51FD;&#x6570;&#x63A5;&#x6536;&#x4E00;&#x4E2A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x5E76;&#x7528;&#x5BF9;&#x4E8E;&#x7A0B;&#x5E8F;&#x5458;&#x6765;&#x8BF4;&#x662F;&#x53EF;&#x9884;&#x77E5;&#x7684;&#x53C2;&#x6570;&#x4F46;&#x5BF9;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x6765;&#x8BF4;&#x662F;&#x4E0D;&#x786E;&#x5B9A;&#x7684;&#x53C2;&#x6570;&#x6765;&#x8C03;&#x7528;&#xFF1A;</p>
<pre><code class="lang-ts">function invokeLater(args: any[], callback: (...args: any[]) =&gt; void) {
    /* ... Invoke callback with &apos;args&apos; ... */
}

// Unsound - invokeLater &quot;might&quot; provide any number of arguments
invokeLater([1, 2], (x, y) =&gt; console.log(x + &apos;, &apos; + y));

// Confusing (x and y are actually required) and undiscoverable
invokeLater([1, 2], (x?, y?) =&gt; console.log(x + &apos;, &apos; + y));
</code></pre>
<h2 id="&#x51FD;&#x6570;&#x91CD;&#x8F7D;">&#x51FD;&#x6570;&#x91CD;&#x8F7D;</h2>
<p>&#x5BF9;&#x4E8E;&#x6709;&#x91CD;&#x8F7D;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x6E90;&#x51FD;&#x6570;&#x7684;&#x6BCF;&#x4E2A;&#x91CD;&#x8F7D;&#x90FD;&#x8981;&#x5728;&#x76EE;&#x6807;&#x51FD;&#x6570;&#x4E0A;&#x627E;&#x5230;&#x5BF9;&#x5E94;&#x7684;&#x51FD;&#x6570;&#x7B7E;&#x540D;&#x3002;
&#x8FD9;&#x786E;&#x4FDD;&#x4E86;&#x76EE;&#x6807;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5728;&#x6240;&#x6709;&#x6E90;&#x51FD;&#x6570;&#x53EF;&#x8C03;&#x7528;&#x7684;&#x5730;&#x65B9;&#x8C03;&#x7528;&#x3002;</p>
<h1 id="&#x679A;&#x4E3E;">&#x679A;&#x4E3E;</h1>
<p>&#x679A;&#x4E3E;&#x7C7B;&#x578B;&#x4E0E;&#x6570;&#x5B57;&#x7C7B;&#x578B;&#x517C;&#x5BB9;&#xFF0C;&#x5E76;&#x4E14;&#x6570;&#x5B57;&#x7C7B;&#x578B;&#x4E0E;&#x679A;&#x4E3E;&#x7C7B;&#x578B;&#x517C;&#x5BB9;&#x3002;&#x4E0D;&#x540C;&#x679A;&#x4E3E;&#x7C7B;&#x578B;&#x4E4B;&#x95F4;&#x662F;&#x4E0D;&#x517C;&#x5BB9;&#x7684;&#x3002;&#x6BD4;&#x5982;&#xFF0C;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">enum</span> Status { Ready, Waiting };
<span class="hljs-keyword">enum</span> Color { Red, Blue, Green };

<span class="hljs-keyword">let</span> status = Status.Ready;
status = Color.Green;  <span class="hljs-comment">// Error</span>
</code></pre>
<h1 id="&#x7C7B;">&#x7C7B;</h1>
<p>&#x7C7B;&#x4E0E;&#x5BF9;&#x8C61;&#x5B57;&#x9762;&#x91CF;&#x548C;&#x63A5;&#x53E3;&#x5DEE;&#x4E0D;&#x591A;&#xFF0C;&#x4F46;&#x6709;&#x4E00;&#x70B9;&#x4E0D;&#x540C;&#xFF1A;&#x7C7B;&#x6709;&#x9759;&#x6001;&#x90E8;&#x5206;&#x548C;&#x5B9E;&#x4F8B;&#x90E8;&#x5206;&#x7684;&#x7C7B;&#x578B;&#x3002;
&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x7C7B;&#x7C7B;&#x578B;&#x7684;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x53EA;&#x6709;&#x5B9E;&#x4F8B;&#x7684;&#x6210;&#x5458;&#x4F1A;&#x88AB;&#x6BD4;&#x8F83;&#x3002;
&#x9759;&#x6001;&#x6210;&#x5458;&#x548C;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E0D;&#x5728;&#x6BD4;&#x8F83;&#x7684;&#x8303;&#x56F4;&#x5185;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">class</span> Animal {
    feet: <span class="hljs-built_in">number</span>;
    <span class="hljs-keyword">constructor</span>(name: string, numFeet: number) { }
}

<span class="hljs-keyword">class</span> Size {
    feet: <span class="hljs-built_in">number</span>;
    <span class="hljs-keyword">constructor</span>(numFeet: number) { }
}

<span class="hljs-keyword">let</span> a: Animal;
<span class="hljs-keyword">let</span> s: Size;

a = s;  <span class="hljs-comment">// OK</span>
s = a;  <span class="hljs-comment">// OK</span>
</code></pre>
<h2 id="&#x7C7B;&#x7684;&#x79C1;&#x6709;&#x6210;&#x5458;&#x548C;&#x53D7;&#x4FDD;&#x62A4;&#x6210;&#x5458;">&#x7C7B;&#x7684;&#x79C1;&#x6709;&#x6210;&#x5458;&#x548C;&#x53D7;&#x4FDD;&#x62A4;&#x6210;&#x5458;</h2>
<p>&#x7C7B;&#x7684;&#x79C1;&#x6709;&#x6210;&#x5458;&#x548C;&#x53D7;&#x4FDD;&#x62A4;&#x6210;&#x5458;&#x4F1A;&#x5F71;&#x54CD;&#x517C;&#x5BB9;&#x6027;&#x3002;
&#x5F53;&#x68C0;&#x67E5;&#x7C7B;&#x5B9E;&#x4F8B;&#x7684;&#x517C;&#x5BB9;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x76EE;&#x6807;&#x7C7B;&#x578B;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x79C1;&#x6709;&#x6210;&#x5458;&#xFF0C;&#x90A3;&#x4E48;&#x6E90;&#x7C7B;&#x578B;&#x5FC5;&#x987B;&#x5305;&#x542B;&#x6765;&#x81EA;&#x540C;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x8FD9;&#x4E2A;&#x79C1;&#x6709;&#x6210;&#x5458;&#x3002;
&#x540C;&#x6837;&#x5730;&#xFF0C;&#x8FD9;&#x6761;&#x89C4;&#x5219;&#x4E5F;&#x9002;&#x7528;&#x4E8E;&#x5305;&#x542B;&#x53D7;&#x4FDD;&#x62A4;&#x6210;&#x5458;&#x5B9E;&#x4F8B;&#x7684;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x3002;
&#x8FD9;&#x5141;&#x8BB8;&#x5B50;&#x7C7B;&#x8D4B;&#x503C;&#x7ED9;&#x7236;&#x7C7B;&#xFF0C;&#x4F46;&#x662F;&#x4E0D;&#x80FD;&#x8D4B;&#x503C;&#x7ED9;&#x5176;&#x5B83;&#x6709;&#x540C;&#x6837;&#x7C7B;&#x578B;&#x7684;&#x7C7B;&#x3002;</p>
<h1 id="&#x6CDB;&#x578B;">&#x6CDB;&#x578B;</h1>
<p>&#x56E0;&#x4E3A;TypeScript&#x662F;&#x7ED3;&#x6784;&#x6027;&#x7684;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#xFF0C;&#x7C7B;&#x578B;&#x53C2;&#x6570;&#x53EA;&#x5F71;&#x54CD;&#x4F7F;&#x7528;&#x5176;&#x505A;&#x4E3A;&#x7C7B;&#x578B;&#x4E00;&#x90E8;&#x5206;&#x7684;&#x7ED3;&#x679C;&#x7C7B;&#x578B;&#x3002;&#x6BD4;&#x5982;&#xFF0C;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> Empty&lt;T&gt; {
}
<span class="hljs-keyword">let</span> x: Empty&lt;<span class="hljs-built_in">number</span>&gt;;
<span class="hljs-keyword">let</span> y: Empty&lt;<span class="hljs-built_in">string</span>&gt;;

x = y;  <span class="hljs-comment">// OK, because y matches structure of x</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x91CC;&#xFF0C;<code>x</code>&#x548C;<code>y</code>&#x662F;&#x517C;&#x5BB9;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x4EEC;&#x7684;&#x7ED3;&#x6784;&#x4F7F;&#x7528;&#x7C7B;&#x578B;&#x53C2;&#x6570;&#x65F6;&#x5E76;&#x6CA1;&#x6709;&#x4EC0;&#x4E48;&#x4E0D;&#x540C;&#x3002;
&#x628A;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x6539;&#x53D8;&#x4E00;&#x4E0B;&#xFF0C;&#x589E;&#x52A0;&#x4E00;&#x4E2A;&#x6210;&#x5458;&#xFF0C;&#x5C31;&#x80FD;&#x770B;&#x51FA;&#x662F;&#x5982;&#x4F55;&#x5DE5;&#x4F5C;&#x7684;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> NotEmpty&lt;T&gt; {
    data: T;
}
<span class="hljs-keyword">let</span> x: NotEmpty&lt;<span class="hljs-built_in">number</span>&gt;;
<span class="hljs-keyword">let</span> y: NotEmpty&lt;<span class="hljs-built_in">string</span>&gt;;

x = y;  <span class="hljs-comment">// Error, because x and y are not compatible</span>
</code></pre>
<p>&#x5728;&#x8FD9;&#x91CC;&#xFF0C;&#x6CDB;&#x578B;&#x7C7B;&#x578B;&#x5728;&#x4F7F;&#x7528;&#x65F6;&#x5C31;&#x597D;&#x6BD4;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x6CDB;&#x578B;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x5BF9;&#x4E8E;&#x6CA1;&#x6307;&#x5B9A;&#x6CDB;&#x578B;&#x7C7B;&#x578B;&#x7684;&#x6CDB;&#x578B;&#x53C2;&#x6570;&#x65F6;&#xFF0C;&#x4F1A;&#x628A;&#x6240;&#x6709;&#x6CDB;&#x578B;&#x53C2;&#x6570;&#x5F53;&#x6210;<code>any</code>&#x6BD4;&#x8F83;&#x3002;
&#x7136;&#x540E;&#x7528;&#x7ED3;&#x679C;&#x7C7B;&#x578B;&#x8FDB;&#x884C;&#x6BD4;&#x8F83;&#xFF0C;&#x5C31;&#x50CF;&#x4E0A;&#x9762;&#x7B2C;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;</p>
<p>&#x6BD4;&#x5982;&#xFF0C;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> identity = <span class="hljs-function"><span class="hljs-keyword">function</span>&lt;<span class="hljs-title">T</span>&gt;(<span class="hljs-params">x: T</span>): <span class="hljs-title">T</span> </span>{
    <span class="hljs-comment">// ...</span>
}

<span class="hljs-keyword">let</span> reverse = <span class="hljs-function"><span class="hljs-keyword">function</span>&lt;<span class="hljs-title">U</span>&gt;(<span class="hljs-params">y: U</span>): <span class="hljs-title">U</span> </span>{
    <span class="hljs-comment">// ...</span>
}

identity = reverse;  <span class="hljs-comment">// OK, because (x: any) =&gt; any matches (y: any) =&gt; any</span>
</code></pre>
<h1 id="&#x9AD8;&#x7EA7;&#x4E3B;&#x9898;">&#x9AD8;&#x7EA7;&#x4E3B;&#x9898;</h1>
<h2 id="&#x5B50;&#x7C7B;&#x578B;&#x4E0E;&#x8D4B;&#x503C;">&#x5B50;&#x7C7B;&#x578B;&#x4E0E;&#x8D4B;&#x503C;</h2>
<p>&#x76EE;&#x524D;&#x4E3A;&#x6B62;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x4E86;&#x201C;&#x517C;&#x5BB9;&#x6027;&#x201D;&#xFF0C;&#x5B83;&#x5728;&#x8BED;&#x8A00;&#x89C4;&#x8303;&#x91CC;&#x6CA1;&#x6709;&#x5B9A;&#x4E49;&#x3002;
&#x5728;TypeScript&#x91CC;&#xFF0C;&#x6709;&#x4E24;&#x79CD;&#x517C;&#x5BB9;&#x6027;&#xFF1A;&#x5B50;&#x7C7B;&#x578B;&#x548C;&#x8D4B;&#x503C;&#x3002;
&#x5B83;&#x4EEC;&#x7684;&#x4E0D;&#x540C;&#x70B9;&#x5728;&#x4E8E;&#xFF0C;&#x8D4B;&#x503C;&#x6269;&#x5C55;&#x4E86;&#x5B50;&#x7C7B;&#x578B;&#x517C;&#x5BB9;&#x6027;&#xFF0C;&#x589E;&#x52A0;&#x4E86;&#x4E00;&#x4E9B;&#x89C4;&#x5219;&#xFF0C;&#x5141;&#x8BB8;&#x548C;<code>any</code>&#x6765;&#x56DE;&#x8D4B;&#x503C;&#xFF0C;&#x4EE5;&#x53CA;<code>enum</code>&#x548C;&#x5BF9;&#x5E94;&#x6570;&#x5B57;&#x503C;&#x4E4B;&#x95F4;&#x7684;&#x6765;&#x56DE;&#x8D4B;&#x503C;&#x3002;</p>
<p>&#x8BED;&#x8A00;&#x91CC;&#x7684;&#x4E0D;&#x540C;&#x5730;&#x65B9;&#x5206;&#x522B;&#x4F7F;&#x7528;&#x4E86;&#x5B83;&#x4EEC;&#x4E4B;&#x4E2D;&#x7684;&#x673A;&#x5236;&#x3002;
&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;&#x7C7B;&#x578B;&#x517C;&#x5BB9;&#x6027;&#x662F;&#x7531;&#x8D4B;&#x503C;&#x517C;&#x5BB9;&#x6027;&#x6765;&#x63A7;&#x5236;&#x7684;&#xFF0C;&#x5373;&#x4F7F;&#x5728;<code>implements</code>&#x548C;<code>extends</code>&#x8BED;&#x53E5;&#x4E5F;&#x4E0D;&#x4F8B;&#x5916;&#x3002;</p>
<p>&#x66F4;&#x591A;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x9605;<a href="https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md" target="_blank">TypeScript&#x8BED;&#x8A00;&#x89C4;&#x8303;</a>.</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Type Inference.html" class="navigation navigation-prev " aria-label="Previous page: 类型推论">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Advanced Types.html" class="navigation navigation-next " aria-label="Next page: 高级类型">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"类型兼容性","level":"1.3.9","depth":2,"next":{"title":"高级类型","level":"1.3.10","depth":2,"path":"doc/handbook/Advanced Types.md","ref":"./doc/handbook/Advanced Types.md","articles":[]},"previous":{"title":"类型推论","level":"1.3.8","depth":2,"path":"doc/handbook/Type Inference.md","ref":"./doc/handbook/Type Inference.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Patrick Zhong","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"preface.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"TypeScript Handbook","language":"zh-hans","gitbook":">3.0.0","description":"Translation of official TypeScript Handbook"},"file":{"path":"doc/handbook/Type Compatibility.md","mtime":"2018-10-13T09:48:54.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-10-13T09:57:42.904Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

